﻿//============================================================
//author:zhxitao
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Saker.Kernel.Model;

namespace Saker.Kernel.DAL
{
    #region SysAuthorityDir数据访问层
	/// <summary>
	///SysAuthorityDir数据访问层
	/// </summary>	
	public partial class SysAuthorityDirDAL
	{
        #region SysAuthorityDir数据访问层 自动生成
		/// <summary>
        /// 添加SysAuthorityDir
        /// </summary>
        /// <param name="SysAuthorityDir">SysAuthorityDir</param>
        /// <returns>添加后的SysAuthorityDir</returns>
        public SysAuthorityDir Add
			(SysAuthorityDir sysAuthorityDir)
		{
				string sql ="INSERT INTO SysAuthorityDir (AuthorityName, AuthorityTag, AuthorityDescription, AuthorityOrder)  VALUES (@AuthorityName, @AuthorityTag, @AuthorityDescription, @AuthorityOrder) select @@identity";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@AuthorityName", ToDBValue(sysAuthorityDir.AuthorityName)),
						new SqlParameter("@AuthorityTag", ToDBValue(sysAuthorityDir.AuthorityTag)),
						new SqlParameter("@AuthorityDescription", ToDBValue(sysAuthorityDir.AuthorityDescription)),
						new SqlParameter("@AuthorityOrder", ToDBValue(sysAuthorityDir.AuthorityOrder)),
					};
					
				int newId = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, para));
				return GetByAuthorityID(newId);
		}

		/// <summary>
        /// 根据主键删除SysAuthorityDir
        /// </summary>
        /// <param name="AuthorityID">AuthorityID</param>
        /// <returns>影响的行数</returns>
        public int DeleteByAuthorityID(int authorityID)
		{
            string sql = "DELETE SysAuthorityDir WHERE AuthorityID = @AuthorityID";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AuthorityID", authorityID)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
		/// <summary>
		/// 更新SysAuthorityDir
		/// </summary>
		/// <param name="SysAuthorityDir">SysAuthorityDir</param>
		/// <returns></returns>
        public int Update(SysAuthorityDir sysAuthorityDir)
        {
            string sql =
                "UPDATE SysAuthorityDir " +
                "SET " +
			" AuthorityName = @AuthorityName" 
                +", AuthorityTag = @AuthorityTag" 
                +", AuthorityDescription = @AuthorityDescription" 
                +", AuthorityOrder = @AuthorityOrder" 
               
            +" WHERE AuthorityID = @AuthorityID";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AuthorityID", sysAuthorityDir.AuthorityID)
					,new SqlParameter("@AuthorityName", ToDBValue(sysAuthorityDir.AuthorityName))
					,new SqlParameter("@AuthorityTag", ToDBValue(sysAuthorityDir.AuthorityTag))
					,new SqlParameter("@AuthorityDescription", ToDBValue(sysAuthorityDir.AuthorityDescription))
					,new SqlParameter("@AuthorityOrder", ToDBValue(sysAuthorityDir.AuthorityOrder))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
		/// <summary>
		/// 根据主键查询SysAuthorityDir实体
		/// </summary>
		/// <param name="SysAuthorityDir">SysAuthorityDir</param>
		/// <returns>SysAuthorityDir</returns>
        public SysAuthorityDir GetByAuthorityID(int authorityID)
        {
            string sql = "SELECT * FROM SysAuthorityDir WHERE AuthorityID = @AuthorityID";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@AuthorityID", authorityID)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		/// <summary>
		/// SqlDataReader对象转换为SysAuthorityDir实体
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysAuthorityDir实体</returns>
		public SysAuthorityDir ToModel(SqlDataReader reader)
		{
			SysAuthorityDir sysAuthorityDir = new SysAuthorityDir();

			sysAuthorityDir.AuthorityID = (int)ToModelValue(reader,"AuthorityID");
			sysAuthorityDir.AuthorityName = (string)ToModelValue(reader,"AuthorityName");
			sysAuthorityDir.AuthorityTag = (string)ToModelValue(reader,"AuthorityTag");
			sysAuthorityDir.AuthorityDescription = (string)ToModelValue(reader,"AuthorityDescription");
			sysAuthorityDir.AuthorityOrder = (int)ToModelValue(reader,"AuthorityOrder");
			return sysAuthorityDir;
		}
		/// <summary>
		/// 得到数据总行数
		/// </summary>
		/// <returns></returns>
		public int GetTotalCount()
		{
			string sql = "SELECT count(1) FROM SysAuthorityDir";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		/// <summary>
		/// 分页，此方法仅在sql 2005下使用
		/// </summary>
		/// <param name="minrownum">起始行数</param>
		/// <param name="maxrownum">>结束行数</param>
		/// <returns>SysAuthorityDir集合</returns>
		public IEnumerable<SysAuthorityDir> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by AuthorityID) rownum FROM SysAuthorityDir) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
        /// <summary>
        /// 分页，调用存储过程
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="whereCondition">where</param>
        /// <returns>DataTable数据集</returns>
        public DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount,string whereCondition)
        {
            return SqlHelper.GetPagingRecord(pageIndex,ref recordCount,pageSize,ref pageCount,"SysAuthorityDir","AuthorityID",whereCondition,"*","AuthorityID",true);
        }
		/// <summary>
		/// 得到所有SysAuthorityDir集合
		/// </summary>
		/// <returns>所有集合</returns>
		public IEnumerable<SysAuthorityDir> GetAll()
		{
			string sql = "SELECT * FROM SysAuthorityDir";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		/// <summary>
		/// SqlDataReader对象转换到SysAuthorityDir集合
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysAuthorityDir集合</returns>
		protected IEnumerable<SysAuthorityDir> ToModels(SqlDataReader reader)
		{
			var list = new List<SysAuthorityDir>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		/// <summary>
		/// 转换数据值
		/// </summary>
		/// <param name="value">object类型</param>
		/// <returns>object对象</returns>
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		/// <summary>
		/// 转换实体值
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <param name="columnName">列名称</param>
		/// <returns>值</returns>
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
        #endregion
	}
    #endregion
}
